package com.shiguiwu.springmybatis.app.controller;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shiguiwu.springmybatis.data.json.ObjToJsonTests;
import com.shiguiwu.springmybatis.mybatis.Book;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description: json测试
 * @author: stone
 * @date: Created by 2022/3/14 9:09
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.app.controller
 */
@RestController
@Slf4j
@RequestMapping("/json")
public class JsonController {

    @GetMapping("/jsonStr")
    public Map<String, Object> jsonStr(@RequestParam String jsonStr) {
        Map map = JSONUtil.toBean(jsonStr, Map.class);

        return map;
    }


    /**
     * 验证 getSexName.不需要字段声明
     * 经过测试是可以的
     *
     * @param jsonStr
     * @return
     */
    @GetMapping("/jsonStr2")
    public ObjToJsonTests.Person jsonStr2(@RequestParam String jsonStr) {

        return new ObjToJsonTests.Person((byte) 1, jsonStr, new Date());
    }


    /**
     * 参数可以是任何
     * @param jsonStr
     * @return
     */
    @PostMapping("/jsonStr3")
    public ObjToJsonTests.Person jsonStr3(@RequestBody @Validated Student jsonStr) {

        log.info("接受的参数是 ==> {}", JSONUtil.toJsonStr(jsonStr));
        return new ObjToJsonTests.Person((byte) 1, "jsonStr", new Date());
    }

    /**
     * 参数校验
     * @param jsonStr
     * @return
     */
    @PostMapping("/valid")
    public ObjToJsonTests.Person valid(@RequestBody @Valid Student jsonStr) {

        log.info("接受的参数是 ==> {}", JSONUtil.toJsonStr(jsonStr));
        return new ObjToJsonTests.Person((byte) 1, "jsonStr", new Date());
    }


    /**
     * 参数校验
     * @param jsonStr
     * @return
     */
    @PostMapping("/valid/list")
    public ObjToJsonTests.Person validList(@RequestBody @Valid Student jsonStr) {

        log.info("接受的参数是 ==> {}", JSONUtil.toJsonStr(jsonStr));
        return new ObjToJsonTests.Person((byte) 1, "jsonStr", new Date());
    }


    @Data
    public static class Student extends ObjToJsonTests.Person {

        /**
         * NotNull可以校验实体，
         * 实体里面的值是否能校验呢？
         * 不加@Valid，实体中的值不肖生
         * 1，加载类上 no
         * 2,加在属性上 yes
         */
        //@Valid
        @NotNull
        private Book book;

        @Valid
        @NotEmpty(message = "list 不能为空")
        private List<Book> books;

        @JsonProperty("SID")
        private String sid;




    }







}
